package com.syyo.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syyo.common.enums.ResultEnum;
import com.syyo.common.exception.SysException;
import com.syyo.admin.domain.req.SysPostReq;
import com.syyo.admin.domain.entity.SysPostEntity;
import com.syyo.admin.domain.export.SysPostExport;
import com.syyo.admin.mapper.SysPostMapper;
import com.syyo.admin.service.SysPostService;

import com.syyo.common.utils.MyFileUtil;
import com.syyo.common.utils.MyStringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: wang
 * @date: 2021-06-03 17:56:30
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPostEntity> implements SysPostService {

    @Autowired
    private SysPostMapper sysPostMapper;

    /**
     * 新增
     */
    @Override
    @Transactional
    public int add(SysPostReq req){
        SysPostEntity entity = new SysPostEntity();
        // 添加创建时间和更新时间
        BeanUtils.copyProperties(req,entity);
        LocalDateTime now = LocalDateTime.now();
        entity.setCreateTime(now);
        int insert = sysPostMapper.insert(entity);
        if (insert != 1){
            throw new SysException(ResultEnum.E_10001.getCode(),ResultEnum.E_10001.getMessage());
        }
        return insert;
    }

    /**
     * 删除
     */
    @Override
    @Transactional
    public int del(SysPostReq req){
        List<Integer> ids = req.getIds();
        boolean b = removeByIds(ids);
        if (!b) {
            throw new SysException(ResultEnum.E_10002.getCode(), ResultEnum.E_10002.getMessage());
        }
        return 1;
    }

    /**
     * 编辑
     */
    @Override
    @Transactional
    public int edit(SysPostReq req){
        SysPostEntity entity = new SysPostEntity();
        BeanUtils.copyProperties(req,entity);
        // 添加更新时间
        int insert = sysPostMapper.updateById(entity);
        if (insert != 1){
            throw new SysException(ResultEnum.E_10003.getCode(),ResultEnum.E_10003.getMessage());
        }
        return insert;
    }

    /**
     * 详情
     */
    @Override
    public SysPostEntity findOne(Integer id){
        return sysPostMapper.selectById(id);
    }

    /**
     * 列表
     */
    @Override
    public IPage<SysPostEntity> findPage(Integer pageNum, Integer pageSize, SysPostReq req){
        Page<SysPostEntity> teacherPage = new Page<SysPostEntity>(pageNum,pageSize);
        QueryWrapper<SysPostEntity> wrapper = new QueryWrapper<>();
        if (MyStringUtils.isNotEmpty(req.getPostName())) {
            //构建条件
            wrapper.like("post_name", req.getPostName());
        }
        return sysPostMapper.selectPage(teacherPage, wrapper);
    }

    /**
     * 导出
     */
    @Override
    public void export(HttpServletResponse response) {
        List<SysPostEntity> entitys = sysPostMapper.selectList(null);
        List<SysPostExport> list = new ArrayList<>();
        for (SysPostEntity entity : entitys) {
            SysPostExport export = new SysPostExport();
            BeanUtils.copyProperties(entity, export);
            list.add(export);
        }
        // 导出文件
        MyFileUtil.exportFile(response, "岗位", list, SysPostExport.class);
    }

    /**
     * 列表,全部
     */
    @Override
    public List<SysPostEntity> findAll() {
        return sysPostMapper.selectList(null);
    }

}